<template>
  <el-dialog
    :title="!dataForm.wmid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="焊机编码" prop="wmbh">
        <el-input v-model="dataForm.wmbh" placeholder="焊机编码"></el-input>
      </el-form-item>
    <el-form-item label="焊机描述" prop="wmnote">
      <el-input v-model="dataForm.wmnote" placeholder="焊机描述"></el-input>
    </el-form-item>
    <el-form-item label="焊机品牌" prop="wmbrand">
      <el-input v-model="dataForm.wmbrand" placeholder="焊机品牌"></el-input>
    </el-form-item>
    <el-form-item label="焊机型号" prop="wmmodel">
      <el-input v-model="dataForm.wmmodel" placeholder="焊机型号"></el-input>
    </el-form-item>
    <el-form-item label="焊机类型" prop="wmtype">
      <el-input v-model="dataForm.wmtype" placeholder="焊机类型"></el-input>
    </el-form-item>
    <el-form-item label="采集模块编码" prop="colmodel">
      <el-input v-model="dataForm.colmodel" placeholder="采集模块编码"></el-input>
    </el-form-item>
      <el-form-item label="MAC地址" prop="wmaddress">
        <el-input v-model="dataForm.wmaddress" placeholder="MAC地址"></el-input>
      </el-form-item>
    <el-form-item label="下次维护时间" prop="nexttime">
      <el-input v-model="dataForm.nexttime" placeholder="下次维护时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          wmid: 0,
          wmbh: '',
          wmnote: '',
          wmbrand: '',
          wmmodel: '',
          wmtype: '',
          colmodel: '',
          wmaddress: '',
          nexttime: ''
        },
        dataRule: {
          wmbh: [
            { required: true, message: '焊机编码不能为空', trigger: 'blur' }
          ],
          wmnote: [
            { required: true, message: '焊机描述不能为空', trigger: 'blur' }
          ],
          wmbrand: [
            { required: true, message: '焊机品牌不能为空', trigger: 'blur' }
          ],
          wmmodel: [
            { required: true, message: '焊机型号不能为空', trigger: 'blur' }
          ],
          wmtype: [
            { required: true, message: '焊机类型不能为空', trigger: 'blur' }
          ],
          colmodel: [
            { required: true, message: '采集模块编码不能为空', trigger: 'blur' }
          ],
          wmaddress: [
            { required: true, message: 'MAC地址不能为空', trigger: 'blur' }
          ],
          nexttime: [
            { required: true, message: '下次维护时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.wmid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.wmid) {
            this.$http({
              url: this.$http.adornUrl(`/weldingmachineinfo/weldingmachineinfo/info/${this.dataForm.wmid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.wmbh = data.weldingmachineinfo.wmbh
                this.dataForm.wmnote = data.weldingmachineinfo.wmnote
                this.dataForm.wmbrand = data.weldingmachineinfo.wmbrand
                this.dataForm.wmmodel = data.weldingmachineinfo.wmmodel
                this.dataForm.wmtype = data.weldingmachineinfo.wmtype
                this.dataForm.colmodel = data.weldingmachineinfo.colmodel
                this.dataForm.wmaddress = data.weldingmachineinfo.wmaddress
                this.dataForm.nexttime = data.weldingmachineinfo.nexttime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/weldingmachineinfo/weldingmachineinfo/${!this.dataForm.wmid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'wmid': this.dataForm.wmid || undefined,
                'wmbh': this.dataForm.wmbh,
                'wmnote': this.dataForm.wmnote,
                'wmbrand': this.dataForm.wmbrand,
                'wmmodel': this.dataForm.wmmodel,
                'wmtype': this.dataForm.wmtype,
                'colmodel': this.dataForm.colmodel,
                'wmaddress': this.dataForm.wmaddress,
                'nexttime': this.dataForm.nexttime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
